package per.zzzfwd.query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompoundValue implements Value, Renderable {

    private ValueConjunction valueConjunction;
    private List<Value> values = new ArrayList<>();

    public CompoundValue(ValueConjunction valueConjunction, Value... values) {
        this.valueConjunction = valueConjunction;
        this.values.addAll(Arrays.asList(values));
    }

    public ValueConjunction getValueConjunction() {
        return valueConjunction;
    }

    public List<Value> getValues() {
        return values;
    }

    @Override
    public String render() {
        StringBuilder builder = new StringBuilder();
        String conjunction = "";
        builder.append("(");
        for (Value value : values) {
            builder
                .append(conjunction)
                .append(((Renderable) value).render());
            conjunction = valueConjunction == ValueConjunction.AND ? " AND " : " OR ";
        }
        builder.append(")");
        return builder.toString();
    }
}
